<?php

class loginController extends AppController
{

    public function loginAction()
    {
        
        $account = (!empty($this->params['account'])) ? $this->params['account'] : "";
        $pwd = (!empty($this->params['pwd'])) ? $this->params['pwd'] : "";
        $validate = (!empty($this->params['valid'])) ? $this->params['valid'] : "";
        $nameFlag = (!empty($this->params['rmp'])) ? $this->params['rmp'] : "";

        $session_validate = !empty($_SESSION["validate"]) ? $_SESSION["validate"] : '';
        # 判断是否接单系统访问 再拼接数据转接单系统
        if(isset($this->params['guest']) && $this->params['guest'] == 'app'){
            $key = $this->params['k'];
            unset($this->params['k']);

            $str = $this->en_str($this->params);
            $str_md5 = md5($str.'zpapp248gash');
            if($str_md5 === $key){
                $where = array("AND" => array("=" => array('store_id' => $this->params['sid'],'business_status' => $this->params['bs'],'company_id' => $this->params['cid'])));
                $xd_store = $this->model->getItemByOther($where,'xd_store');
                if($xd_store){
                    if (empty($xd_store[0]["status"])) {
                        $msg =  '请与管理者联系';
                        echo "<script>alert('".$msg."');window.close();</script>";
                        return;
                    } else if ($xd_store[0]["business_status"]!=2 && $xd_store[0]["business_status"]!=4 && $xd_store[0]["business_status"]!=5){
                        $msg = '您门店状态是'.Data::$_store_business_status[$xd_store[0]['business_status']].'，无法登录，请与管理者联系';
                        echo "<script>alert('".$msg."');window.close();</script>";
                        return;
                    }else {
                        $this->params['guest'] = 'zp';
                        $str = $this->en_str($this->params);
                        $key = md5($str . 'zpapp248gash');
                        $str .= '&k=' . $key;
                        $_SESSION['zp_shop']['guest'] = $str;
                        $_SESSION['zp_shop']['store_id'] = $xd_store[0]['store_id'];
                        $_SESSION['zp_shop']['store_name'] = $xd_store[0]['storename'];
                        $_SESSION['zp_shop']['company_id'] = $xd_store[0]['company_id'];
                        $_SESSION['zp_shop']['islogin'] = true;
                        $_SESSION['login']['count'] = 0;
                        $_SESSION['zp_shop']['time'] = date('Y-m-d H:i', time());
                    }
                }else{
                    echo "<script>alert('门店异常!');window.close();</script>";
                    return;
                }

            }else{

                echo "<script>alert('无权登录!');window.close();</script>";
                return;
            }

        }


        if (!empty($_SESSION["zp_shop"]) && count($_SESSION["zp_shop"]) > 0 && $_SESSION['zp_shop']['islogin'] == true) {
            Tools::AlertTxt('index.php?md=shop&cl=shop&at=home', '');
            return;
        }

        $this->view->setVariable("account", $account);
        $this->view->render();
    }

    public function logincheckAction()
    {

        $account = (!empty($this->params['account'])) ? $this->params['account'] : "";
        $pwd = (!empty($this->params['pwd'])) ? $this->params['pwd'] : "";
        $validate = (!empty($this->params['valid'])) ? $this->params['valid'] : "";
        $session_validate = !empty($_SESSION['validate']) ?$_SESSION['validate'] : '';

        $ajax_result = array("status" => 0, "msg" => '');

        if (empty($account) || empty($pwd)) {
            $ajax_result["msg"] = '请输入帐号密码';
       // } else if (empty($validate)) {
       //     $ajax_result["msg"] = '请输入验证码';
       // } else if (strtolower($validate) != strtolower($session_validate)) {
       //     $ajax_result["msg"] = '验证码输入错误';
        } else {

            $rows = $this->model->loginCheck($account, $pwd);

            if (empty($rows) || $rows['isdeleted'] == 1) {
                $ajax_result["msg"] = '无此帐号';
            } else
                if (empty($rows["status"])) {
                    $ajax_result["msg"] = '请与管理者联系';
                } else if ($rows["business_status"]!=2 && $rows["business_status"]!=4 && $rows["business_status"]!=5){
                    $ajax_result["msg"] = '您门店状态是'.Data::$_store_business_status[$rows['business_status']].'，无法登录，请与管理者联系';
                }else

                    if ($rows['password'] == sha1($rows['salt'] . $pwd)) {
                        $_SESSION['zp_shop']['store_id'] = $rows['store_id'];
                        $_SESSION['zp_shop']['store_name'] = $rows['storename'];
                        $_SESSION['zp_shop']['company_id'] = $rows['company_id'];
                        $_SESSION['zp_shop']['islogin'] = true;
                        $_SESSION['login']['count'] = 0;
                        $_SESSION['zp_shop']['time'] = date('Y-m-d H:i', time());


                        $ajax_result["status"] = '1';
                        $ajax_result["msg"] = '登录成功';
                        $ajax_result["url"] = 'index.php?md=shop&cl=shop&at=home';

                        # 访问接单系统数据
                        $rows['guest'] = 'zp';
                        $str = $this->en_str($rows);
                        $key = md5($str.'zpapp248gash');
                        $str .= '&k='.$key;
                        $_SESSION['zp_shop']['guest'] = $str;


                        $table='log_shop_login';
                        //新增
                        $field=array();
                        $field['shop_no']=$rows['store_id'];
                        $field['ip']=Tools::getIP();
                        $field["isdeleted"] = 0;
                        $field['createdt'] = 'now()';
                        $r = $this->model->addItem($field, $table);


                    } else {
                        $ajax_result["msg"] = '帐号或密码有误';
                    }
        }

        echo json_encode($ajax_result, JSON_UNESCAPED_UNICODE);
        exit;
    }

    private function en_str($rows){
        $data['sid'] = isset($rows['store_id']) ? $rows['store_id'] : $rows['sid'];
        $data['cid'] = isset($rows['company_id']) ? $rows['company_id'] : $rows['cid'];
        $data['bs'] = isset($rows['business_status']) ? $rows['business_status'] : $rows['bs'];
        $data['guest'] = $rows['guest'];
        $data['t'] = isset($rows['t']) ? $rows['t'] : time();
        ksort($data);
        $str = '';
        foreach ($data as $k => $itm){
            $str.= '&' . $k .'='. $itm;
        }
        return $str;
    }

    public function logoutAction()
    {
        if (isset($_SESSION['zp_shop']) && !empty($_SESSION['zp_shop'])) {
            unset($_SESSION['zp_shop']);
        }
        $this->loginAction();
    }



}

?>
